<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<link rel="stylesheet" type="text/css" href="public/css/style.css"> 
<?php
require_once './model/db.php';


$query = sprintf("SELECT * FROM pages
     INNER JON apps_pages ON pages.id = apps_pages.id_page
     INNER JON apps ON apps_pages.id_app = apps.id
     INNER JON companies ON apps.id_company = companies.id
     WHERE (apps.id_company = Company) AND (apps.id = App)
     LIMT 0,%u;", PER_PAGE);
$pages = getPages($query);
$companies = getCompanies("SELECT * FROM companies");
$apps = getApps("SELECT * FROM apps");
$keyword = NULL;
$Min = NULL;
$Max = NULL;

var_dump($query);
?>

<form method="POST">
    Tu Khoa  : <input type="text" name="keyword" style="border-color:  #ff33cc"><br>
    Cong Ty :  <select name="Company" style="border-color: #ccff00">

        <?php foreach ($companies as $company): ?>
            <option value="<?php echo $company['id'] ?>"><?php echo $company["name"] ?></option>
        <?php endforeach; ?>
    </select> <br>
    Ung Dung&nbsp;:<select name="App" style="border-color: #00ccff">

        <?php foreach ($apps as $app): ?>
            <option value="<?php echo $app['pid'] ?>"><?php echo $app["name"] ?></option>
        <?php endforeach; ?>
    </select> <br>
    Min Like &nbsp;: <input type="text" name="Min" style="border-color: #ff0000"><br>
    Max Like : <input type="text" name="Max" style="border-color: #0000ff"><br>
    <input type="submit" name="submit" value="Search">
</form>
<?php if (isset($_POST['submit'])): ?>
    <?php $keyword = $_POST['keyword']; ?>
    <?php $Min = (double) $_POST['Min']; ?>
    <?php $Max = (double) $_POST['Max']; ?>
    <?php $apps = $_POST['App'] ?>
    <?php $companies = $_POST['Company']; ?>

<?php endif; ?>

<table id="table1" border="1px" style="border-color: #ff9900">
    <tr class="dien">
        <td class="pid">STT </td>
        <td class="id">id </td>
        <td class="name">name </td>
        <td class="likes">likes </td>
        <td class="talking">talking</td>
        <td class="category">category </td>
        <td class="link">link </td>
        <td class="website">website </td>
        <td class="picture">picture </td>
        <td class="country">country </td>
    </tr>
    <?php foreach ($pages as $page): ?>
        <?php if (empty($keyword)): ?>
            <?php if (empty($Min) && empty($Max)): ?>
                <tr>
                    <td><?php echo $page["pid"] ?></td>
                    <td><?php echo $page["id"] ?></td>
                    <td><?php echo $page["name"] ?></td>
                    <td><?php echo $page["likes"] ?></td>
                    <td><?php echo $page["talking_about_count"] ?></td>
                    <td><?php echo $page["category"] ?></td>
                    <td><a href='<?php echo $page["link"] ?>'><?php echo substr($page["link"], 0, 50) ?></td>
                    <td><a href='<?php echo $page["website"] ?>'><?php echo substr($page["website"], 0, 50) ?></td>
                    <td><img src ='<?php echo $page["picture"] ?>'</td>
                    <td><?php echo $page["country"] ?></td>
                </tr>
            <?php elseif (empty($Max)): ?>
                <?php if ($Min <= (double) $page["likes"]): ?>
                    <tr>
                        <td><?php echo $page["pid"] ?></td>
                        <td><?php echo $page["id"] ?></td>
                        <td><?php echo $page["name"] ?></td>
                        <td><?php echo $page["likes"] ?></td>
                        <td><?php echo $page["talking_about_count"] ?></td>
                        <td><?php echo $page["category"] ?></td>
                        <td><a href='<?php echo $page["link"] ?>'><?php echo substr($page["link"], 0, 50) ?></td>
                        <td><a href='<?php echo $page["website"] ?>'><?php echo substr($page["website"], 0, 50) ?></td>
                        <td><img src ='<?php echo $page["picture"] ?>'</td>
                        <td><?php echo $page["country"] ?></td>
                    </tr>
                <?php endif; ?>
            <?php else: ?>
                <?php if (($Min <= (double) $page["likes"]) && ($Max > $page["likes"])): ?>
                    <tr>
                        <td><?php echo $page["pid"] ?></td>
                        <td><?php echo $page["id"] ?></td>
                        <td><?php echo $page["name"] ?></td>
                        <td><?php echo $page["likes"] ?></td>
                        <td><?php echo $page["talking_about_count"] ?></td>
                        <td><?php echo $page["category"] ?></td>
                        <td><a href='<?php echo $page["link"] ?>'><?php echo substr($page["link"], 0, 50) ?></td>
                        <td><a href='<?php echo $page["website"] ?>'><?php echo substr($page["website"], 0, 50) ?></td>
                        <td><img src ='<?php echo $page["picture"] ?>'</td>
                        <td><?php echo $page["country"] ?></td>
                    </tr>
                <?php endif; ?>
            <?php endif; ?>
        <?php else: ?>
            <?php if (strstr($page["name"], $keyword)): ?>
                <?php if (empty($Min) && empty($Max)): ?>
                    <tr>
                        <td><?php echo $page["pid"] ?></td>
                        <td><?php echo $page["id"] ?></td>
                        <td><?php echo $page["name"] ?></td>
                        <td><?php echo $page["likes"] ?></td>
                        <td><?php echo $page["talking_about_count"] ?></td>
                        <td><?php echo $page["category"] ?></td>
                        <td><a href='<?php echo $page["link"] ?>'><?php echo substr($page["link"], 0, 50) ?></td>
                        <td><a href='<?php echo $page["website"] ?>'><?php echo substr($page["website"], 0, 50) ?></td>
                        <td><img src ='<?php echo $page["picture"] ?>'</td>
                        <td><?php echo $page["country"] ?></td>
                    </tr>
                <?php elseif (empty($Max)): ?>
                    <?php if ($Min <= (double) $page["likes"]): ?>
                        <tr>
                            <td><?php echo $page["pid"] ?></td>
                            <td><?php echo $page["id"] ?></td>
                            <td><?php echo $page["name"] ?></td>
                            <td><?php echo $page["likes"] ?></td>
                            <td><?php echo $page["talking_about_count"] ?></td>
                            <td><?php echo $page["category"] ?></td>
                            <td><a href='<?php echo $page["link"] ?>'><?php echo substr($page["link"], 0, 50) ?></td>
                            <td><a href='<?php echo $page["website"] ?>'><?php echo substr($page["website"], 0, 50) ?></td>
                            <td><img src ='<?php echo $page["picture"] ?>'</td>
                            <td><?php echo $page["country"] ?></td>
                        </tr>
                    <?php endif; ?>
                <?php else: ?>
                    <?php if (($Min <= (double) $page["likes"]) && ($Max > $page["likes"])): ?>
                        <tr>
                            <td><?php echo $page["pid"] ?></td>
                            <td><?php echo $page["id"] ?></td>
                            <td><?php echo $page["name"] ?></td>
                            <td><?php echo $page["likes"] ?></td>
                            <td><?php echo $page["talking_about_count"] ?></td>
                            <td><?php echo $page["category"] ?></td>
                            <td><a href='<?php echo $page["link"] ?>'><?php echo substr($page["link"], 0, 50) ?></td>
                            <td><a href='<?php echo $page["website"] ?>'><?php echo substr($page["website"], 0, 50) ?></td>
                            <td><img src ='<?php echo $page["picture"] ?>'</td>
                            <td><?php echo $page["country"] ?></td>
                        </tr>
                    <?php endif; ?>
                <?php endif; ?>
            <?php endif; ?> 
        <?php endif; ?> 
    <?php endforeach; ?>
</table>